基于SQL Server中如何比较两个表的各组数据 图解说明
完整代码:
针对方法1,需要对原表增加一个字段dataPath,用于存储data列数据连串的结果,如:
检查表table_left的各组(groupId),是否在表table_right中存在有一组(groupId)数据(data)与它的数据(data)完全相等.
cross apply(select (select '-'+x.data from table_right x where x.groupId=a.groupId order by x.data for xml path('')) as dataPath)bselect '#3','data92' union all

分析
select '#5','data77' union allselect '#11','data2' union all
View Code
use tempdb
where exists(select 1 from table_right x where x.dataPath=a.dataPath)
select '#4','data65' union all
set dataPath=b.dataPath
from table_left a
分组连串data列数据并update至刚新增的列dataPath,如:
复制代码 代码如下:
create table table_right(groupId nvarchar(5),data nvarchar(10))cross apply(select (select '-'+x.data from table_left x where x.groupId=a.groupId order by x.data for xml path('')) as dataPath)b
update a
select '#1','data1' union all
select '#33','data99' union all
from table_left a
set nocount on
if object_id('table_right') is not null drop table table_right
方法2:
select '#2','data55' union allgo
select '#1','data3' union all
select '#2','data55' union all
go
在数据库中,要找出表table_left和表table_right存在相同数据的组,方法很多,这里我列出两种常用的方法。
select '#1','data2' union all复制代码 代码如下:
goif object_id('table_left') is not null drop table table_left
select '#4','data67' union all
set dataPath=b.dataPath
select '#5','data79'
from table_left a
go
select '#55','data77' union all

select '#22','data57' union all
select '#4','data66' union all
from table_right a
复制代码 代码如下:
insert into table_right(groupId,data)图3.
select distinct a.groupIdselect distinct a.groupId
(下面的SQL脚本,是以图4.的数据为基础参考)
图2.
通过SQL Sever提供的集运算符"Except",判断两组非重复的数据。如果两组针对对方都不存在非重复的数据,就说明这两组数据完全相等。如,表table_left中的组#11和表 table_right中的组#1,对列data进行"Except"集运算,无任是(#11 à #1)进行Except集运算,还是(#1 à #11 )进行Except集合运算,都返回空结果,这就说明组#1 和#11的data数据完全相等,如:
create nonclustered index ix_left on table_left(dataPath)select '#44','data68' union all
insert into table_left(groupId,data)
通过"Select … From …Order by … xml for path('') "把各组的data列数据连串起来(如,图4.把table_left的组#11的列data连串起来成"data1-data2-data3"),其他分组(包含表table_right)以此方法实现data列数据连串起来;然后通过比较两表的连串后字段是否存在相等,若是相等就说明这比较多两组数据相等,由此可以判断出表table_left的哪组数据在表table_right存在与它数据完全相等的组。
select '#33','data99' union allset dataPath=b.dataPath
复制代码 代码如下:
create table table_left(groupId nvarchar(5),data nvarchar(10))where exists(select 1 from table_right x where x.dataPath=a.dataPath)
cross apply(select (select '-'+x.data from table_left x where x.groupId=a.groupId order by x.data for xml path('')) as dataPath)b

前一阵子,在项目中碰到这样一个SQL查询需求,有两个相同结构的表(table_left & table_right),如下:
from table_left aupdate a
select '#4','data69' union all
--
从上面的两个表,可以知道它们存放的是一组一组的数据;那么,接下来我借助数学集合的列举法和运算进行分析。
如图1. 可以看出表table_left和table_right存在两组数据完整相等:
update a
图1.
方法1:
alter table table_left add dataPath nvarchar(200)select '#55','data79'
先通过集合的列举法描述两个表的各组数据:
select '#11','data1' union all图4.
update ago

set dataPath=b.dataPath
接下来就是查询了,如:
from table_right aselect '#4','data68' union all
select '#22','data55' union all
select '#3','data91' union all
实现
复制代码 代码如下:
alter table table_right add dataPath nvarchar(200)select '#44','data66' union all
select '#11','data3' union all
alter table table_left add dataPath nvarchar(200)
go
alter table table_right add dataPath nvarchar(200)
这里只有两种情况,相等和不相等。对于不相等,可再分为部分相等、包含、和完全不相等。使用集合描述,可使用交集,子集,并集。如下面图4.,我列举出这几种常见的情况:
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/sql/mssql/3120.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
SQL基本教程之行转列Pivo
时间:2021-01-20
-
region from hr.Employees union
时间:2021-01-20
-
有时候需要调整用户权限
时间:2021-01-19
-
(但使用 ORDER BY 子句并不
时间:2021-01-19
-
RAND()*10000)insert into Detail
时间:2021-01-19
-
OR 运算符:在两侧的查询
时间:2021-01-19
-
放假之前老大跟我提起了
时间:2021-01-19
-
数据库的运维计策剧本篇
时间:2021-01-19
热门文章
-
4.与聚合函数和 GROUP BY 子句有关的常见错
时间:2021-01-19
-
SQL Server安全(11/11):审核(Auditing)
时间:2021-01-09
-
sqlserver中查询横表变竖表的sql语句简析
时间:2020-12-08
-
SQL Server简单模式下误删除堆表记录恢复方
时间:2020-12-12
-
关于SQL Server查询语句的使用
时间:2020-12-13
-
MSSQL教程_mssql数据库教程_MSSQL基础教程_第
时间:2020-12-13
-
jdbc连接sql server数据库问题分析
时间:2020-12-10
-
详解SQL游标的用法
时间:2020-12-27
-
sql server 关于设置null的一些建议
时间:2020-12-28
-
mssql关于一个表格结构的另外一种显示(表
时间:2020-12-11
